We will use mainly three libraries.

library(jpeg)
library(ggplot2)
library(EBImage)

First we read the image and check the structure and dimensions.

img <- readJPEG("/Users/ayberkakgun/Desktop/doku3.jpeg")
str(img)
##  num [1:512, 1:512, 1:3] 0.325 0.247 0.22 0.294 0.447 ...
dim(img)
## [1] 512 512   3

The image can be displayed with “rasterImage” function.

plot(c(0, 512), c(0, 512), type = "n", xlab = "", ylab = "")
rasterImage(img, 0, 0, 512, 512)

We split the image to channels; red, blue and green.

For each channel, we take the average of the columns with “colMeans” and plot the average as a line for each channel.

We chose to crop the image horizontally for each channel and than substract the lower part from the upper part.

#Median Filtering

Let’s check the raw image once again.

We use medianFilter function to filter and display again. First for n=5.

Than for n=11.

and lastly for n=31.